#Include Plugin.ahk
#Include ../core/Hook.ahk

class TimestampPlugin extends Plugin {

  __New(timeformat := 'yyyyMMdd_HHmmss', TRP := 150, color := 'FFFFFF', bgc := '000000') {
    this.timeformat := timeformat
    this.TRP := TRP
    this.color := color
    this.bgc := bgc
  }

  Run() {
    Hook.Register(Events.OnTimestampGenerate, DisplayTimestamp)

    DisplayTimestamp(g, *) {
      ts := TimestampPlugin.Timestamp.Build(g.createTime, this.timeformat, this.color, this.bgc)
      g.GetPos(&x, &y, &w, &h)
      ts.Show(x, y, this.TRP)
      ts.GetPos(, , &tw, &th)
      tx := x + w - tw
      ty := y + h - th
      ts.Move(tx, ty)
      Sleep 30
      DrawTimestamp(g, w - tw, h - th, tx, ty, tw, th)
      ts.Hide()

      DrawTimestamp(target, x, y, tx, ty, w, h) {
        hdc_frame := DllCall('GetDC', 'ptr', 0)
        hdd_frame := DllCall('GetDC', 'ptr', target.Hwnd)
        DllCall("StretchBlt"
          , 'ptr', hdd_frame, 'int', x, 'int', y, 'int', w, 'int', h
          , 'ptr', hdc_frame, 'int', tx, 'int', ty, 'int', w, 'int', h
          , 'UInt', 0xCC0020)
        DllCall('ReleaseDC', 'int', 0, 'ptr', hdc_frame)
        DllCall('ReleaseDC', 'int', 0, 'ptr', hdd_frame)
      }
    }
  }

  class Timestamp extends Gui {

    __New(timeformat, color, bgc) {
      timeString := FormatTime(, timeformat)
      super.__New('+AlwaysOnTop -Caption +Border +ToolWindow')
      this.BackColor := bgc
      this.SetFont('s11', 'consolas')
      this.AddText('vtime c' color, timeString)
    }

    static Build(time, timeformat, color, bgc) {
      static g := TimestampPlugin.Timestamp(timeformat, color, bgc)
      g['time'].value := FormatTime(time, timeformat)
      return g
    }

    Show(x, y, TRP := 150) {
      WinSetTransparent(TRP, this)
      super.Show('x' x ' y' y)
    }
  }

}